package WbComment.file;

import cn.hutool.log.Log;
import cn.hutool.log.LogFactory;

import javax.swing.*;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

/**
 * 窗体
 */
public class Window extends JFrame implements iFrame {
    private static final Log log = LogFactory.get();
    // 相关参数
    private String title = "微博爬取";
    private int width = 350; // 宽度
    private int height = 300; // 高度
    // 输出用
    private JTextArea jTextArea_out;
    // 自身
    private Window self;

    // 构造
    public Window() {
        self = this;
        this.setTitle(title);//设置窗体标题
        setSize(width, height);//设置窗体尺寸
        // 【cookie】
        JLabel jLabel_cookie = new JLabel("cookie:");
        jLabel_cookie.setBounds(50, 20, 50, 20);
        this.add(jLabel_cookie);
        // 【地址】
        JLabel jLabel_add = new JLabel("地址:");
        jLabel_add.setBounds(50, 50, 50, 20);
        this.add(jLabel_add);
        // 【cookie】 -- 输入框
        final JTextField input_cookie = new JTextField(20);
        input_cookie.setBounds(110, 20, 120, 20);
        this.add(input_cookie);
        // 【地址】 -- 输入框
        final JTextField input_address = new JTextField(20);
        input_address.setBounds(110, 50, 120, 20);
        this.add(input_address);
        this.setLayout(null);
        // 启动按钮
        JButton process = new JButton("启动");
        process.setBounds(240, 20, 80, 48);
        this.add(process);

        // 输出显示
        jTextArea_out = new JTextArea();
        jTextArea_out.setLineWrap(true);
        // 输出，滚动条
        JScrollPane scroll = new JScrollPane(jTextArea_out);
        scroll.setBounds(10, 80, 320, 180);
        this.add(scroll);

        // 按钮部分
        process.addActionListener(event->{
            // 取得输入内容
            String strCookie = input_cookie.getText();
            String strAddress = input_address.getText();
//            if(null == strCookie || "".equals(strCookie)){
//                JOptionPane.showMessageDialog(null,"请输入cookie");
//                return ;
//            }
            if(null == strAddress || "".equals(strAddress)){
                JOptionPane.showMessageDialog(null,"请输入爬取地址");
                return ;
            }
            // 爬虫处理
            new Thread(()->{
                new Spider(self, strCookie, strAddress).process();
            }).start();
        });
        this.setVisible(true);
        // JFrame打开后居中
        setLocationRelativeTo(getOwner());
        // 添加窗体关闭事件
        this.addWindowListener(new WindowAdapter(){
            @Override
            public void windowClosing(WindowEvent ev){
                setDefaultCloseOperation(DISPOSE_ON_CLOSE);
            }
        });
    }
    // 输出信息
    @Override
    public void showMsg(String msg){
        log.info(msg);
        this.jTextArea_out.append(msg);
        this.jTextArea_out.append("\n");
    }
}
